/**
 * 
 */
Ext.namespace("Ext.lams");
Ext.lams.RoleGrid = Ext.extend(Ext.grid.GridPanel, {
			constructor : function(config) {
				Ext.apply(config, {
							loadMask : {
								msg : '正在载入数据,请稍等...'
							}

						});
				Ext.lams.RoleGrid.superclass.constructor.call(this, config
								|| {})
				this.addEvents("assignPermissons");
			},
			initComponent : function() {
				var me = this;
				var _store = new Ext.data.JsonStore({
							proxy : new Ext.data.HttpProxy({
										url : "getRoleAction.action",
										method : "GET"
									}),
							totalProperty : "totalCount",
							root : "data",
							fields : [{
										name : 'ROLE_ID'
									}, {
										name : 'ROLE_NAME'
									}, {
										name : 'REMARK'
									}, {
										name : 'CREATOR'
									}, {
										name : 'CREAT_DATE'
									}]
						});
				var tbar = [{
							xtype : "btn_new",
							handler : function() {

							}
						}, '-', {
							xtype : "btn_edit",
							handler : function() {

							}
						}, '-', {
							text : '权限分配',
							width : '75',
							handler : function() {
								me.assignPermissions();
							},
							iconCls : ''
						}, '-', {
							xtype : "btn_delete",
							width : '75',
							handler : function() {

							}
						}];
				var bbar = new Ext.lams.comp.PagingToolbar({
							store : _store
						});
				var cm = new Ext.grid.ColumnModel([{
							header : '角色编号',
							hidden : true,
							dataIndex : 'ROLE_ID'
						}, {
							header : '角色名称',
							dataIndex : 'ROLE_NAME'
						}, {
							header : '创建人',
							dataIndex : 'CREATOR'
						}, {
							header : '创建时间',
							dataIndex : 'CREAT_DATE'
						}, {
							header : '角色说明',
							dataIndex : 'REMARK'
						}]);
				var sm = new Ext.grid.RowSelectionModel({
							singleSelect : true
						});
				Ext.apply(this, {
							store : _store,
							tbar : tbar,
							bbar : bbar,
							sm : sm,
							cm : cm
						});
				_store.load({
							params : {
								start : 0,
								limit : 20
							}
						});
				Ext.lams.RoleGrid.superclass.initComponent.call(this);
			},
			assignPermissions : function() {
				try {
					var _r = this.getSelectRow();
					this.fireEvent("assignPermissions", this, _r);
				} catch (_error) {
					// Ext.MessageBox.alert("ERROR", _error.message);
					H.Msg.error(_error.message);
				}
			},
			getSelectRow : function() {
				var sel = this.getSelectionModel();
				if (sel.getCount() == 0)
					throw new Error("未选中某条记录");
				return sel.getSelected();
			}
		});
Ext.lams.RoleInfoPanel = Ext.extend(Ext.form.FormPanel, {
			constructor : function(config) {
				Ext.apply(this, config || {});
				Ext.lams.RoleInfoPanel.superclass.constructor.call(this, {
							layout : 'form',
							frame : true,
							defaults : {
								anchor : '100%',
								labelWidth : 65,
								xtype : 'textfield'
							}
						});
			},
			initComponent : function() {
				var _store = new Ext.data.JsonStore({
							proxy : new Ext.data.HttpProxy({
										url : 'getUserListByRole.action',
										method : 'GET'
									}),
							totalProperty : 'totalCount',
							root : 'data',
							fields : [{
										name : 'NAME'
									}, {
										name : 'NO'
									}, {
										name : 'ID'
									}]
						});
				this.store = _store;
				this.items = [{
							fieldLabel : '角色编号',
							name : 'ROLE_ID',
							hidden : true

						}, {
							fieldLabel : '角色名称',
							name : 'ROLE_NAME'

						}, {
							xtype : 'textarea',
							fieldLabel : '角色说明',
							name : 'REMARK'

						}, {
							fieldLabel : '创建人',
							disabled : true,
							name : 'CREATOR'
						}, {
							fieldLabel : '创建时间',
							disabled : true,
							name : 'CREAT_DATE'
						}, {
							xtype : 'panel',
							frame : true,
							title : '用户列表',
							// width : 535,
							autoHeight : true,
							collapsible : true,
							layout : 'fit',
							items : [{
								xtype : 'dataview',
								store : _store,
								tpl : new Ext.XTemplate(
										'<tpl for=".">',
										'<div clas="x-view-over" id="{ID}">',
										'<span>{NAME}</span>--<span>{NO}</span></div>',
										'</tpl>'),
								autoHeight : true,
								multiSelect : true,
								overClass : 'x-view-over',
								itemSelector : 'x-view-selected'
							}]
						}];
				Ext.lams.RoleInfoPanel.superclass.initComponent.call(this);
			},
			setValue : function(_r) {
				this.getForm().loadRecord(_r);
				this.store.load({
							params : {
								'ROLE_ID' : _r.data['ROLE_ID']
							}
						});
			},
			getValue : function() {
				if (this.getForm().isValid())
					return new Ext.data.Record(this.getForm().getValues());
				else
					throw Error("表单填写出错");
			}
		});
Ext.lams.ModuleAndFunPanel = Ext.extend(Ext.form.FormPanel, {
	constructor : function(_cfg) {
		Ext.apply(this, _cfg || {});
		Ext.lams.ModuleAndFunPanel.superclass.constructor.call(this, {
					layout : 'form',
					frame : true
				});
	},
	initComponent : function() {
		var data;
		var me = this;
		Ext.Ajax.request({
			url : 'showAllMFAction.action',
			method : 'GET',
			params : this.params,
			waitMsg : "获取数据...",
			async : false,
			success : function(response, action) {
				var result = Ext.util.JSON.decode(response.responseText.trim());
				if (result['success']) {
					data = result["data"];
				}
			}
		});
		this.items = this.lovpData(data);
		Ext.lams.ModuleAndFunPanel.superclass.initComponent.call(this);
	},
	lovpData : function(data) {
		var items = [];
		var flag = false;
		for (var i = 0; i < data.length; i++) {
			var item = data[i];
			var children = item["children"];
			var type = item["expanded"];
			var id = item["id"];
			var text = item["text"];
			var checked = item["checked"];
			if (type) {
				var fieldSet = new Ext.form.FieldSet({
							checkboxToggle : true,
							checkboxName : 'm_' + id,
							collapsed : !checked,
							title : text,
							items : this.lovpData(children)
						});
				items.push(fieldSet);
			} else if (!type) {
				var checkbox = new Ext.form.Checkbox({
							boxLabel : text,
							name : 'f_' + id,
							checked : checked
						});
				items.push(checkbox);
				flag = true;
				// return boxs
			}
		}
		if (flag) {
			var cg = new Ext.form.CheckboxGroup({
						items : items
					});
			var temp = [];
			temp.push(cg);
			return temp;
		}
		return items;
	},
	saveValues : function(rid) {
		var mask = new Ext.LoadMask(Ext.getBody(), {
					msg : "正在保存数据..."
				});
		mask.show();
		var values = this.getForm().getValues();
		var data = [];
		for (var item in values) {
			if (item.indexOf('f') == 0) {
				data.push(item.substring(2, item.length))
			}
		}
		// var params = {'FUNCTIONS':data,'ROLE_ID':rid};
		Ext.apply(this.params, {
					'FUNCTIONS' : data
				});
		var r;
		Ext.Ajax.request({
			url : 'saveFunctionAction.action',
			method : 'POST',
			params : this.params,
			// waitMsg:"保存数据...",
			async : false,
			success : function(response, action) {
				mask.hide()
				var result = Ext.util.JSON.decode(response.responseText.trim());
				// Ext.Msg.alert("提示",result["message"]);
				H.Msg.info(result["message"]);
				r = result["success"];
			}
		});
		return r;
		// Ext.Msg.alert(data);
	}
});
Ext.lams.AssignPermissionWin = Ext.extend(Ext.Window, {
			form : null,
			constructor : function(config) {
				Ext.apply(this, config)
				Ext.lams.AssignPermissionWin.superclass.constructor.call(this,
						{
							title : '权限分配',
							width : 600,
							height : 400,
							autoScroll : true
						});
			},
			initComponent : function() {
				var me = this;
				this.buttons = [{
							xtype : 'btn_save',
							handler : function() {
								if (me.form.saveValues()) {
									me.close();
								}
							},
							scope : this
						}, {
							xtype : 'btn_cancel',
							handler : function() {
								me.close()
							},
							scope : this
						}];
				Ext.lams.AssignPermissionWin.superclass.initComponent.call(this);
			}
		});
Ext.lams.RoleManagerPanel = Ext.extend(Ext.Panel, {
			constructor : function(config) {
				// Ext.apply(this, config || {});
				Ext.lams.RoleManagerPanel.superclass.constructor.call(this,
						config || {});
			},
			initComponent : function() {
				var me = this;
				// var assignForm = null;
				var win;
				var _grid = new Ext.lams.RoleGrid({
							region : 'center',
							split : true,
							listeners : {
								rowclick : function(g, n, e) {
									var _r = g.getStore().getAt(n);
									me.infoform.setValue(_r);
								},
								assignPermissions : function(g, r) {
									win = new Ext.lams.AssignPermissionWin();
									win.form = new Ext.lams.ModuleAndFunPanel({
												params : {
													'ROLE_ID' : r.data['ROLE_ID']
												}
											})
									// win.removeAll();
									win.add(win.form);
									win.show();
								}
							}
						});
				var _form = new Ext.lams.RoleInfoPanel({
							region : 'east',
							width : 300,
							title : '角色信息',
							collapseMode : 'mini',
							split : true,
							autoScroll : true,
							buttons : [{
										xtype : 'btn_save'
									}, {
										xtype : 'btn_cancel'
									}]
						});
				this.grid = _grid;
				this.infoform = _form;
				Ext.apply(this, {
							layout : 'border',
							items : [me.grid, me.infoform]
						})
				Ext.lams.RoleManagerPanel.superclass.initComponent.call(this);
			}
		})